package com.googlecode.flair.components
{
	import com.googlecode.flair.DesignAdvisor;
	
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.core.Container;
	import mx.core.UIComponent;

	[Bindable]
	public class BaseComponent extends Canvas implements IFlairComponent
	{
		public var content : Container = null;
		
		public var parentComponent : IFlairComponent = null;
		public var components : ArrayCollection = new ArrayCollection();
		
		public function BaseComponent()
		{
			content = this;
		}
		
		
		public final function removeFromParent() : void {
			if (parentComponent != null) {
				parentComponent.remove(IFlairComponent(this));
			}
		}
		
		public final function remove(component : IFlairComponent) : void {
			var index : int = components.getItemIndex(component);
			if ( index == -1 ) {
				return;
			}
			var uiComponent : UIComponent = component as UIComponent;
			BaseComponent(component).parentComponent = this;
			content.removeChild(uiComponent);
			components.removeItemAt(index);
		} 
		
		public final function add(component:IFlairComponent) : void
		{
			var uiComponent : UIComponent = component as UIComponent;
			if (components.length == 0) {
				content.removeAllChildren();
			}
			content.addChild(uiComponent);
			components.addItem(component);
			component.parentComponent = this;
		}
		
		public function get componentName() : String 
		{
			var componentName : String = getQualifiedClassName(this);
			componentName = componentName.substring(componentName.indexOf("::") + 2);
			return componentName;
		}
		
		
		
		public final function select() : void
		{
			DesignAdvisor.instance.advisedComponent = this;
		}
		
	}
}